#include <Ogre.h> 
#include <CEGUI/CEGUI.h> 
#include <list>

#include "include/App.h"
#include "include/GuiEvents.h"

using namespace Ogre;
using namespace Ei;

///////// GUI implementation ////////////////////////////////

GuiEvents::GuiEvents() 
{


}
GuiEvents::~GuiEvents()
{	
	
}

bool GuiEvents::reinitKalman(const CEGUI::EventArgs& e)
{

	App::magAcc()->resetFilters(int(App::gui()->mTuneAcc->isSelected()));
	
	return true;
	
}


bool GuiEvents::saveSettings(const CEGUI::EventArgs& e)
{
	
	////////////////////////////////////////////////////
	//// this will save the slider settings very ugly //
	///  in a file format ... I need to change this   //
	///  to saving the actual values stored in my     //
	///  kalman filters ...... but for now, till the  //
	///  correct engine is around it will do 	  //
	////////////////////////////////////////////////////
	ConfigFile cf;
	cf.load("filters.cfg");
	
	std::ofstream os;
	os.open("filters.cfg");
	
	//define what filter to save from silder values
	String sildersec = "";
	if(App::gui()->mTuneAcc->isSelected())
       		sildersec = "acc";
	if(App::gui()->mTuneMag->isSelected())
       		sildersec = "magstation";       
	if(App::gui()->mTuneMagMove->isSelected())
       		sildersec = "magmove";       
       
	ConfigFile::SectionIterator seci = cf.getSectionIterator();
	while(seci.hasMoreElements()) {
		String secName = seci.peekNextKey();
		ConfigFile::SettingsMultiMap* settings = seci.getNext();
		ConfigFile::SettingsMultiMap::iterator i;
		
	if(secName.compare(sildersec) == 0)
         {
        	os << "\n";
         	os << "[" << secName << "]";
 	     	os << "\n";
 	     	if(secName.compare("acc") == 0)
         	{
		os << "filter"    << "="  << Math::Exp((App::gui()->mAccFilterBar->getScrollPosition()) -1) * 0.0001  <<"\n";
	     	os << "interstep" << "=" << App::gui()->mAccInterStepBar->getScrollPosition() + 0.001 <<"\n";
	     	os << "interamount" << "=" << App::gui()->mAccInterAmountBar->getScrollPosition()+ 0.001 << "\n";
		}
		else
		{		
		os <<  "switch" << "=" << ((Math::Exp(App::gui()->mAccFilterBar->getScrollPosition())) -1) * 0.0001 <<"\n";
	     	os << "max" << "=" <<     (Math::Exp(App::gui()->mAccInterStepBar->getScrollPosition()) - 1) * 0.0001 <<"\n";	
		}
	     	os <<  "dt" << "=" << float(Math::Exp(App::gui()->mdt->getScrollPosition()) -1) << "\n";	
	     	os <<  "V_GAMMA" << "=" << float(Math::Exp(App::gui()->mV_GAMMA->getScrollPosition()) -1) <<"\n";
	     	os <<  "V_NOISE" << "=" << float(Math::Exp(App::gui()->mV_NOISE->getScrollPosition()) -1) <<"\n";
	     	os <<  "OBS_NOISE" << "=" << float(Math::Exp(App::gui()->mOBS_NOISE->getScrollPosition()) -1) <<"\n";
		os << "\n";     
         }
        else
         {      
		 if (!secName.empty())
		 {	  
		 	os << "\n";
         	 	os << "[" << secName << "]";
 	     	 	os << "\n";   
		 	for (i = settings->begin(); i != settings->end(); ++i)
		 	{ 
				os << i->first << "=" << StringConverter::parseReal(i->second) << "\n";										
		 	}
			os << "\n";
		}	
         }		
	}// end while
   
	eilog("settings saved");
	
	return true;
	
}
bool GuiEvents::loadSettings(const CEGUI::EventArgs& e)
{
	ConfigFile cf;
	cf.load("filters.cfg");
	
	String sildersec = "";
	if(App::gui()->mTuneAcc->isSelected())
       		sildersec = "acc";
	if(App::gui()->mTuneMag->isSelected())
       		sildersec = "magstation";       
	if(App::gui()->mTuneMagMove->isSelected())
       		sildersec = "magmove";  
		       
		       
	ConfigFile::SectionIterator seci = cf.getSectionIterator();
	std::map<std::string, Real> tempSettings; 
	while(seci.hasMoreElements()) {
		String secName = seci.peekNextKey();
		ConfigFile::SettingsMultiMap* settings = seci.getNext();
		ConfigFile::SettingsMultiMap::iterator i;
		
	if(secName.compare(sildersec) == 0)
         {
	   for (i = settings->begin(); i != settings->end(); ++i)
	    { 
       		tempSettings[i->first] = StringConverter::parseReal(i->second);	               
	    }
	 }		
	}// end while
	
	if(sildersec.compare("acc") == 0)
        {
	     	App::gui()->mAccFilterBar->setScrollPosition((Math::Log(tempSettings["filter"] + 1))* 1000 );	
		App::gui()->mAccInterStepBar->setScrollPosition(tempSettings["interstep"]);
		App::gui()->mAccInterAmountBar->setScrollPosition(tempSettings["interamount"]);
	}
	else
	{	
		App::gui()->mAccFilterBar->setScrollPosition((Math::Log(tempSettings["switch"] + 1)) * 10000);	
		App::gui()->mAccInterStepBar->setScrollPosition((Math::Log(tempSettings["max"] + 1)) * 10000);
	}
	     	App::gui()->mdt->setScrollPosition(Math::Log(tempSettings["dt"] +1));
	     	App::gui()->mV_GAMMA->setScrollPosition(Math::Log(tempSettings["V_GAMMA"]+ 1) );
		App::gui()->mV_NOISE->setScrollPosition(Math::Log(tempSettings["V_NOISE"]+ 1) );
		App::gui()->mOBS_NOISE->setScrollPosition(Math::Log(tempSettings["OBS_NOISE"]+ 1) );
	
	return true;
}
